package com.taobao.txc.resourcemanager.b;

import com.taobao.txc.common.LoggerInit;
import com.taobao.txc.common.LoggerWrap;
import com.taobao.txc.common.d;
import com.taobao.txc.common.j;
import com.taobao.txc.parser.struct.TxcRuntimeContext;
import com.taobao.txc.parser.struct.m;
import com.taobao.txc.resourcemanager.a.f;
import com.taobao.txc.resourcemanager.b.a.a;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

public class b
{
  private static final LoggerWrap a = LoggerInit.logger;
  private static final ConcurrentHashMap<String, com.taobao.txc.resourcemanager.b.a.b> b = new ConcurrentHashMap();
  private static final Set<String> c = Collections.synchronizedSet(new HashSet());
  private static final Map<com.taobao.txc.resourcemanager.b.a.b, Map<String, String>> d = new ConcurrentHashMap();
  private static ConcurrentHashMap<String, List<com.taobao.txc.resourcemanager.b.a.b>> e = new ConcurrentHashMap();
  
  public static Map<String, com.taobao.txc.resourcemanager.b.a.b> a()
  {
    return b;
  }
  
  public static com.taobao.txc.resourcemanager.b.a.b a(String paramString)
  {
    com.taobao.txc.resourcemanager.b.a.b localb = null;
    if ((localb = (com.taobao.txc.resourcemanager.b.a.b)b.get(paramString)) == null) {
      throw new com.taobao.txc.common.b.c("can not find datasource by dbName:[" + paramString + "]");
    }
    return localb;
  }
  
  public void a(com.taobao.txc.resourcemanager.b.a.b paramb, String paramString1, String paramString2)
  {
    synchronized (b.class)
    {
      if (!b.containsKey(paramString1)) {
        b.put(paramString1, paramb);
      }
    }
    synchronized (e)
    {
      if (!StringUtils.isEmpty(paramString2))
      {
        List localList = (List)e.get(paramString2);
        if (localList == null)
        {
          e.putIfAbsent(paramString2, new LinkedList());
          localList = (List)e.get(paramString2);
        }
        localList.add(paramb);
      }
    }
    ??? = com.taobao.txc.resourcemanager.c.a();
    if (??? != null) {
      ((com.taobao.txc.resourcemanager.c)???).c(paramString1);
    }
    a(paramb);
  }
  
  public static void a(com.taobao.txc.resourcemanager.b.a.b paramb)
  {
    if (paramb == null)
    {
      a.error(com.taobao.txc.common.b.b.b.bd, "setRmRpcClient:[invalid datasource]");
      return;
    }
    try
    {
      JdbcTemplate localJdbcTemplate1 = new JdbcTemplate(paramb.a());
      c.addAll(f.a(localJdbcTemplate1));
    }
    catch (Exception localException1)
    {
      a.warn(com.taobao.txc.common.b.b.B.bd, "db:" + paramb.b() + " not exist txc_undo_log." + localException1.getMessage());
    }
    try
    {
      JdbcTemplate localJdbcTemplate2 = new JdbcTemplate(paramb.a());
      ConcurrentHashMap localConcurrentHashMap = new ConcurrentHashMap();
      d.put(paramb, localConcurrentHashMap);
      Set localSet = f.b(localJdbcTemplate2);
      Iterator localIterator = localSet.iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        localConcurrentHashMap.put(str, str);
      }
    }
    catch (Exception localException2)
    {
      a.warn(com.taobao.txc.common.b.b.B.bd, "db:" + paramb.b() + " not exist txc_undo_log." + localException2.getMessage());
    }
  }
  
  public static Set<String> b()
  {
    synchronized (b.class)
    {
      Iterator localIterator = b.keySet().iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        a((com.taobao.txc.resourcemanager.b.a.b)b.get(str));
      }
      if (a.c()) {
        a.debug("server address:" + c);
      }
      return c;
    }
  }
  
  public static String c()
  {
    synchronized (b.class)
    {
      StringBuffer localStringBuffer = new StringBuffer();
      Set localSet = b.keySet();
      Iterator localIterator = localSet.iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        localStringBuffer.append(str).append(',');
      }
      return localStringBuffer.substring(0, localStringBuffer.length() > 0 ? localStringBuffer.length() - 1 : 0);
    }
  }
  
  public static final void a(com.taobao.txc.resourcemanager.b.a.b paramb, String paramString)
  {
    Map localMap = (Map)d.get(paramb);
    if (!localMap.containsKey(paramString))
    {
      long l = -d.b(paramString);
      TxcRuntimeContext localTxcRuntimeContext = new TxcRuntimeContext();
      localTxcRuntimeContext.d(l);
      localTxcRuntimeContext.b(paramString);
      localTxcRuntimeContext.a(m.d.a());
      localTxcRuntimeContext.a(new com.taobao.txc.parser.struct.c());
      localTxcRuntimeContext.a(j.a(-1L));
      a locala = null;
      try
      {
        locala = (a)paramb.getConnection();
        f.a(locala, localTxcRuntimeContext);
      }
      catch (SQLException localSQLException)
      {
        if (localSQLException.getErrorCode() == 1062)
        {
          JdbcTemplate localJdbcTemplate = new JdbcTemplate(paramb.a());
          List localList = f.a(localJdbcTemplate, localTxcRuntimeContext.a(), localTxcRuntimeContext.b());
          if (localList.size() == 0) {
            throw localSQLException;
          }
        }
        else
        {
          throw localSQLException;
        }
      }
      finally
      {
        if (locala != null) {
          locala.close();
        }
      }
      localMap.put(paramString, paramString);
    }
  }
  
  public static final Map<com.taobao.txc.resourcemanager.b.a.b, Map<String, String>> d()
  {
    return d;
  }
  
  public static Iterator<com.taobao.txc.resourcemanager.b.a.b> b(com.taobao.txc.resourcemanager.b.a.b paramb)
  {
    LinkedList localLinkedList = new LinkedList();
    synchronized (e)
    {
      List localList = (List)e.get(paramb.e());
      if (localList != null)
      {
        Iterator localIterator = localList.iterator();
        while (localIterator.hasNext())
        {
          com.taobao.txc.resourcemanager.b.a.b localb = (com.taobao.txc.resourcemanager.b.a.b)localIterator.next();
          if (localb != paramb) {
            localLinkedList.add(localb);
          }
        }
      }
    }
    return localLinkedList.iterator();
  }
}


/* Location:           C:\Users\ibm\Downloads\txc-yun-sample (1)\txc-yun-sample\lib\txc-client-2.0.72.jar
 * Qualified Name:     com.taobao.txc.resourcemanager.b.b
 * JD-Core Version:    0.7.0.1
 */